//
//  ContentView.swift
//  DentalFormula
//
//  Created by AMalashin on 22.09.2023.
//

import SwiftUI
import Combine
import Speech

struct ContentView: View {
    let languageId = Bundle.main.preferredLocalizations[0]
    let nerprocessor = NERProcessor()
    @Environment(\.displayScale) var displayScale
    @StateObject var speechRecognizer = SpeechRecognizer()
    @State private var recognizing: Bool = false
    @State private var text: String = String(localized: "msg.press-green-button")
    @State private var teeth: [Tooth] = []
    @State private var screenShot = Image(systemName: "photo")
    
    private var formulaView: some View {
        FormulaView(teeth: $teeth).padding()
    }
    
    var body: some View {
        VStack {
            HStack {
                Button(action: { teeth = [] }) { Image(systemName: "goforward") }
                Spacer()
                ShareLink("", item: screenShot, preview: SharePreview(Text("Shared image"), image: screenShot))
            }.padding(.horizontal)
            VStack {
                formulaView //FormulaView(teeth: $teeth).padding()
                Text(text)
                    .fixedSize(horizontal: false, vertical: true)
                    .multilineTextAlignment(.center)
                    .lineLimit(5)
                    .font(.title3)
                    .padding(.horizontal)
                    .frame(width: 400, height: 150)
                    .background(
                        RoundedRectangle(cornerRadius: 20)
                            .stroke()
                            .fill(.tertiary)
                            .padding(.horizontal)
                    )
                DictateButtonView(active: $recognizing, start: startRecording, stop: stopRecording)
                Text(languageId)
            }
            .onChange(of: teeth) { takeScreenshot() }
            //.onAppear { takeScreenshot() }
        }
    }

    func startRecording() {
        print("DentalFormula: start recording pressed")
        speechRecognizer.startListening() { msg in
            self.text = msg
            let addon = nerprocessor.extract(from: msg)
            for tooth in addon {
                if let idx = teeth.firstIndex(where: { $0.number == tooth.number }) { // replace an item if it exists already...
                    teeth[idx] = tooth
                } else { // ...or add a new one
                    teeth.append(tooth)
                }
            }
        }
    }

    func stopRecording() {
        print("DentalFormula: stop recording pressed")
        speechRecognizer.stopListening() { msg in text = msg }
//        text = String(localized: "msg.press-green-button")
    }
    
    @MainActor func takeScreenshot() {
        let ir = ImageRenderer(content: formulaView)
        ir.scale = displayScale
        
        if let img = ir.uiImage {
            screenShot = Image(uiImage: img)
        }
    }
}

#Preview("English localization") {
    ContentView().environment(\.locale, .init(identifier: "en"))
}

#Preview("Russian localization") {
    ContentView().environment(\.locale, .init(identifier: "ru"))
}
